using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using CMS.BLL;
using CMS.BLL.Entity;
public partial class AdminX_Adv_AdvAdd : System.Web.UI.UserControl
{
   private  int nDefaultLanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        MessAlert1.HideMessage();
       
        if (!IsPostBack)
        {
            SetLabelLanguage();
            BindDataToPositionDropdown();
            ConfigCkeditor();
            SetValidate();
            InitData();
        }
    }

    private void SetLabelLanguage()
    {

        lblClient.Text = MultipleLanguage.PARTNERNAME[nDefaultLanguage-1];
        lblContent.Text = MultipleLanguage.CONTENT[nDefaultLanguage-1];
        lblImage.Text = MultipleLanguage.LBL_IMAGE[nDefaultLanguage-1];
        lblLink.Text = MultipleLanguage.LBL_URL[nDefaultLanguage-1];
        lblPosition.Text = MultipleLanguage.Position[nDefaultLanguage-1];

        lbleActive.Text = MultipleLanguage.LBL_ACTIVE[nDefaultLanguage-1];
        lbldisplayOrder.Text = MultipleLanguage.LBL_PRIORITY[nDefaultLanguage - 1];

        btnCancel.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage-1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage-1];
        lblExampleLink.Text = MultipleLanguage.ExampleLink[nDefaultLanguage - 1];
        //lblBestDisplay.Text = MultipleLanguage.BestGood[nDefaultLanguage - 1];
        //lkback.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage - 1];
    }

    protected void ConfigCkeditor()
    {

        txtcontent.Language = MultipleLanguage.CKEDITOR_LANGUAGE[nDefaultLanguage];
        txtcontent.config.filebrowserImageBrowseUrl = Config.FILEBROWSERIMAGE_BROWSE;
        txtcontent.config.filebrowserImageUploadUrl = Config.FILEBROWSERIMAGE_UPLOAD;
        CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
        _FileBrowser.BasePath = Config.FILEBROWSER;
        _FileBrowser.SetupCKEditor(txtcontent);
    }
    #region Bind Data Dropdown
    private void BindDataToPositionDropdown()
    {
        int nCurrentLang = ConvertData.ConvertToInt(ddlPosition .SelectedValue);
        DataTable dtbCategory = new DataTable();
        AdvPosition objAdvPosition = new AdvPosition();
        dtbCategory = objAdvPosition.Search();
        ddlPosition .DataSource = dtbCategory;
        ddlPosition.DataValueField = "AdvPositionID";
        ddlPosition.DataTextField = "Description";
        ddlPosition.DataBind();
    }
    #endregion

    #region InitData
    private void SetValidate()
    {
        rfvTitle.ErrorMessage = MessagesValidate.ADV_CLIENT_EMPTY;
        rfvLink.ErrorMessage = MessagesValidate.ADV_PATH_EMPTY;
    }

    private void InitData()
    {
        string sQuerry = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        int nAdvID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        try
        {
            if (sQuerry == Config.ACTION_EDIT && nAdvID > 0)
            {
                int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nItemID > 0)
                    GetData(nItemID);
            }
            else
            {
                imgImage.ImageUrl = Config.IMAGE_NO_IMAGE;
  
            }
        }
        catch
        {
            string sRedirect = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_ADV  + "&" + Config.MESS_ID + "=" + Config.ITEM_NOTEXIST;
            Response.Redirect(sRedirect);
        }

    }

    private void GetData(int ItemID)
    {
        // Get data to view on UI Controls
        int nStatus = 0;
        Adv objAdv = new Adv();
        objAdv.LoadById(ItemID);

        txtClient.Text = objAdv.Data.PartnerName;

        txtcontent.Text=(ConvertData.ConvertToString(objAdv.Data.Description ));
        txtLink.Text = ConvertData.ConvertToString(objAdv.Data.AdvURL);
        nStatus = ConvertData.ConvertToInt(objAdv.Data.Status);
        if (nStatus > 0)
            chkActive.Checked = true;
        else
            chkActive.Checked = false;

       int  nPriority = ConvertData.ConvertToInt(objAdv.Data.Priority);
        if (nPriority > 0)
            chkDisplayOrder.Checked = true;
        else
            chkDisplayOrder.Checked = false;

        string sImage = ConvertData.ConvertToString(objAdv.Data.AdvFile );
        lbimg.Text = ConvertData.ConvertToString(objAdv.Data.AdvFile);
        urlImg.NavigateUrl = Config.ROOT + Config.IMAGE_ADV_DEFAULT_UPLOAD + objAdv.Data.AdvFile;
        if (sImage != "")
            imgImage.ImageUrl = Config.ROOT + Config.IMAGE_ADV_DEFAULT_UPLOAD  + objAdv.Data.AdvFile ;
        else imgImage.ImageUrl = Config.IMAGE_NO_IMAGE;
    }

    private eAdv AdvEntity()
    {
        eAdv entityAdv = new eAdv();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            Adv obj = new Adv();
            obj.LoadById(nItemID);
            entityAdv = obj.Data;
        }
        return entityAdv;
    }

    private eAdv SetData()
    {
        eAdv eAdvEntity = this.AdvEntity ();
        Adv objAdv = new Adv();
        if (iptImage.Value != "")
            objAdv.Data.AdvFile  = Support.UploadFile(iptImage , "adv");
        else
        {
            if (eAdvEntity.AdvFile  != "")
                objAdv.Data.AdvFile  = eAdvEntity.AdvFile ;
            else objAdv.Data.AdvFile  = "";
        }
        objAdv.Data.PartnerName = ConvertData.ConvertToString(txtClient.Text  ); 
        objAdv.Data.Description  = ConvertData.ConvertToString(txtcontent.Text);
        objAdv.Data.AdvPositionID  = ConvertData.ConvertToInt(ddlPosition.SelectedValue  );
        objAdv.Data.AdvTypeID  = 1;

        if (chkActive.Checked) objAdv.Data.Status = 1;
        else objAdv.Data.Status = 0;

        if (chkDisplayOrder.Checked) objAdv.Data.Priority = 1;
        else objAdv.Data.Priority = 0;

        objAdv.Data.AdvURL  = ConvertData.ConvertToString(txtLink.Text  );
        objAdv.Data.CreatedDate  = ConvertData.ConvertDatetimeToInt(DateTime.Today );
        objAdv.Data.ExpiredDate = 20990101 ;
      
        return objAdv.Data;
    }
    #endregion
    private string GetQuerryString(string sQuerry)
    {
        string sQuerryString = "";
        sQuerryString = ConvertData.ConvertToString(Request.QueryString[sQuerry]);
        return sQuerryString;
    }
    #region Click Events
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string sGoToPageMan = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_ADV ;
        Response.Redirect(sGoToPageMan);
    }
   
  
   
    protected void btnSave_Click1(object sender, EventArgs e)
    {
        int nActionResult = 0;
        int nDefaultLang = Support.GetDefaultLanguage()-1; 
        Adv objAdv = new Adv();
        objAdv.Data = this.SetData();
        string sAction = GetQuerryString(Config.ACTION);
        if (sAction == Config.ACTION_EDIT)
        {
            //Is Edit
            int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
            nActionResult = objAdv.Update(nItemID);
            if (nActionResult > 0)
            {
                 
                if (objAdv.Data.AdvFile.ToString() != "")
                {
                    imgImage.ImageUrl = Config.ROOT + Config.IMAGE_ADV_DEFAULT_UPLOAD + objAdv.Data.AdvFile.ToString();
                    lbimg.Text = ConvertData.ConvertToString(objAdv.Data.AdvFile);
                    urlImg.NavigateUrl = Config.ROOT + Config.IMAGE_ADV_DEFAULT_UPLOAD + objAdv.Data.AdvFile;
                }
                MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);
               
                //string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_ADV + "&" + Config.MESS_ID + "=" + Config.MESSAGE_UPDATE;
                //Response.Redirect(sURL);
            }
            else
                MessageBoxss.Show(MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLang]);
              
        }
        else
        {
            // Is Insert
            nActionResult = objAdv.Insert();

            if (nActionResult > 0)
            {
               string sURL= Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_ADV + "&" + Config.MESS_ID + "=" + Config.MESSAGE_INSERT;
                Response.Redirect(sURL);
                
            }

        }
    }


    #endregion

   
}
